package top.xiaoyu.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * JDBC Demo
 * 下载MySQL驱动并导入，maven的话直接引入依赖即可
 * 在代码中将驱动包进行加载
 * 将驱动包交给驱动管理器，由驱动管理器获取数据库连接
 * 由数据库连接获取数据库操作对象
 * sql中有?进行替换
 * 执行sql
 * 关闭connection和statement
 */
public class MainTest03 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "Dong1314520.";
        Connection connection = DriverManager.getConnection(url, user, password);
        PreparedStatement preparedStatement = connection.prepareStatement("update t_user set age=? where id=?");
        preparedStatement.setInt(1, 20);
        preparedStatement.setInt(2, 3);
        preparedStatement.execute();
        preparedStatement.close();
        connection.close();
    }
}
